let express = require('express');
let app = express();
let router = express.Router();
let cookie = require("cookie-parser");
router.use(cookie())
// console.log(cookie);

//加载验证码包
let svgCaptcha = require("svg-captcha");
// console.log(svgCaptcha);

//读取persons.json文件
let fs = require("fs");
let persons = fs.readFileSync(__dirname + "/persons.json", "utf8");
persons = JSON.parse(persons);

// 规划路由，生出验证码
router.get("/svgcode",(req,res)=>{
    //生成验证码
    let Captcha = svgCaptcha.create();
    // console.log(Captcha);
    //使用cookie存储验证码的值
    res.type("svg");
    res.send(Captcha.data);
    console.log(res.cookie);
    res.cookie("ct",Captcha.text);
})
router.post("/svgcode",(req,res)=>{
    
})

// //规划路由  加载登录页面
router.get('/login',(req,res)=>{
    res.render('pages/loginAndRegister/login.html')
})

//处理表单数据
router.post("/login", (req, res) => {
    let { tel, pass, code } = req.body;
    // console.log(Captcha.text);
    let u = persons.find((item) => {
        return item.username == tel && item.password == pass;
    })
    if (u) {
        res.send("登录成功")
    } else {
        res.send("登录失败")
    }

})

module.exports = router;